package com.example.sbeducation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sbeducation.dto.HomeworkDTO;
import com.example.sbeducation.entity.Homework;
import com.example.sbeducation.entity.HomeworkSubmission;
import com.example.sbeducation.entity.User;
import com.example.sbeducation.exception.BusinessException;
import com.example.sbeducation.mapper.HomeworkMapper;
import com.example.sbeducation.mapper.HomeworkSubmissionMapper;
import com.example.sbeducation.service.HomeworkService;
import com.example.sbeducation.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作业服务实现类
 */
@Service
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements HomeworkService {

  @Autowired
  private UserService userService;

  @Autowired
  private HomeworkSubmissionMapper homeworkSubmissionMapper;

  @Autowired
  private HomeworkMapper homeworkMapper;

  @Override
  @Transactional
  public Long createHomework(HomeworkDTO homeworkDTO) {
    // 验证教师是否存在
    User teacher = userService.getUserById(homeworkDTO.getTeacherId());
    if (!"teacher".equals(teacher.getRole())) {
      throw new BusinessException(400, "只有教师才能发布作业");
    }

    // 创建作业
    Homework homework = new Homework();
    BeanUtils.copyProperties(homeworkDTO, homework);
    homework.setCreateTime(LocalDateTime.now());
    homework.setUpdateTime(LocalDateTime.now());
    homework.setDeleted(0);

    // 保存
    save(homework);

    return homework.getId();
  }

  @Override
  @Transactional
  public void updateHomework(HomeworkDTO homeworkDTO) {
    // 验证作业是否存在
    Homework homework = getById(homeworkDTO.getId());
    if (homework == null) {
      throw new BusinessException(400, "作业不存在");
    }

    // 验证教师是否有权限修改
    if (!homework.getTeacherId().equals(homeworkDTO.getTeacherId())) {
      throw new BusinessException(400, "无权修改该作业");
    }

    // 更新作业
    BeanUtils.copyProperties(homeworkDTO, homework);
    homework.setUpdateTime(LocalDateTime.now());

    // 保存
    updateById(homework);
  }

  @Override
  @Transactional
  public void deleteHomework(Long homeworkId) {
    // 验证作业是否存在
    Homework homework = getById(homeworkId);
    if (homework == null) {
      throw new BusinessException(400, "作业不存在");
    }

    // 删除作业
    removeById(homeworkId);

    // 删除作业相关的提交记录
    LambdaQueryWrapper<HomeworkSubmission> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(HomeworkSubmission::getHomeworkId, homeworkId);
    homeworkSubmissionMapper.delete(queryWrapper);
  }

  @Override
  public Homework getHomeworkById(Long homeworkId) {
    Homework homework = getById(homeworkId);
    if (homework == null) {
      throw new BusinessException(400, "作业不存在");
    }
    return homework;
  }

  @Override
  public HomeworkDTO getHomeworkDetail(Long homeworkId) {
    // 获取作业基本信息
    Homework homework = getHomeworkById(homeworkId);

    // 转换为DTO
    HomeworkDTO homeworkDTO = new HomeworkDTO();
    BeanUtils.copyProperties(homework, homeworkDTO);

    // 可以在这里添加额外的业务逻辑，如获取教师信息等
    User teacher = userService.getUserById(homework.getTeacherId());
    if (teacher != null) {
      homeworkDTO.setTeacherName(teacher.getRealName());
    }

    return homeworkDTO;
  }

  @Override
  public Page<Homework> getTeacherHomeworkList(Long teacherId, String title, Integer page, Integer size) {
    User teacher = userService.getUserById(teacherId);
    if (!"teacher".equals(teacher.getRole())) {
      throw new BusinessException("只有教师可以查看自己发布的作业");
    }

    Page<Homework> pageParam = new Page<>(page, size);
    LambdaQueryWrapper<Homework> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(Homework::getTeacherId, teacherId);

    // 添加标题搜索条件
    if (StringUtils.hasText(title)) {
      wrapper.like(Homework::getTitle, title);
    }

    wrapper.orderByDesc(Homework::getCreateTime);

    return homeworkMapper.selectPage(pageParam, wrapper);
  }

  @Override
  public Page<Homework> getStudentHomeworkList(Long studentId, Integer page, Integer size) {
    // 验证学生是否存在
    User student = userService.getUserById(studentId);
    if (!"student".equals(student.getRole())) {
      throw new BusinessException(400, "只有学生才能查看作业列表");
    }

    // 查询所有作业
    Page<Homework> pageParam = new Page<>(page, size);
    LambdaQueryWrapper<Homework> queryWrapper = new LambdaQueryWrapper<>();
    // 添加逻辑删除过滤条件
    queryWrapper.eq(Homework::getDeleted, 0);
    queryWrapper.orderByDesc(Homework::getCreateTime);

    return page(pageParam, queryWrapper);
  }

  @Override
  public Page<HomeworkDTO> getStudentHomeworkList(Long studentId, String title, Integer page, Integer size) {
    User student = userService.getUserById(studentId);
    if (!"student".equals(student.getRole())) {
      throw new BusinessException("只有学生可以查看作业列表");
    }

    Page<HomeworkDTO> pageParam = new Page<>(page, size);
    return homeworkMapper.getStudentHomeworkList(pageParam, studentId, title);
  }
}